/***

    {
    "name": "Container",
    "margin": "s|10,50",
    "padding": "a|10",
    "alignment": "centerLeft",
    "height": "150",
    "width": "300",
    "decoration": {
    "color": "0xff123456",
    "borderRadius": 10.5
    },
    "child": {
    "mainAxisAlignment": "center",
    "crossAxisAlignment": "center",
    "children": [
    {
    "name": "Text",
    "text": "center",
    "textStyle": {
    "color": "0xff654325",
    "fontSize": 20.5,
    "fontWeight": "FontWeight.bold"
    }
    },
    {
    "name": "Expanded",
    "flex": "2",
    "child": {
    "name": "Container",
    "margin": "s|10,50",
    "padding": "a|10",
    "alignment": "centerLeft",
    "height": "150",
    "width": "300",
    "decoration": "color|0xff123456,radius|10"
    }
    },
    {
    "name": "Image",
    "type": "netWork",
    "height": "120",
    "width": "200",
    "src": "http://pic41.nipic.com/20140508/18609517_112216473140_2.jpg",
    "fit": "fill"
    }
    ]
    }
    }


 */



import 'dart:convert' show json;

import 'package:flutter/material.dart';


String jsonStr = '''
{
    "name": "Container",
    "margin": {
        "left": 10,
        "top": 20,
        "right": 32,
        "bottom": 10
    },
    "padding": {
        "left": 10,
        "top": 20,
        "right": 32,
        "bottom": 10
    },
    "alignment": "centerLeft",
    "height": 550,
    "width": 300,
    "decoration": {
        "name": "BoxDecoration",
        "color": "0xff123456",
        "borderRadius": 10.5
    },
    "child": {
        "name": "Container",
        "margin": {
            "left": 10,
            "top": 20,
            "right": 32,
            "bottom": 10
        },
        "padding": {
            "left": 10,
            "top": 20,
            "right": 32,
            "bottom": 10
        },
        "alignment": "centerLeft",
        "height": 150,
        "width": 300,
        "decoration": {
            "name": "BoxDecoration",
            "color": "0xff123456",
            "borderRadius": 50.5
        },
        "child": {
            "name": "Column",
            "mainAxisAlignment": "center",
            "crossAxisAlignment": "center",
            "children": [
                {
                    "name": "Text",
                    "text": "center",
                    "textStyle": {
                        "color": "0xffffffff",
                        "fontSize": 50.5,
                        "fontWeight": "FontWeight.bold"
                    }
                },
                {
                    "name": "Expanded",
                    "flex": "2",
                    "child": {
                        "name": "Container",
                        "margin": {
                            "left": 10,
                            "top": 20,
                            "right": 32,
                            "bottom": 10
                        },
                        "padding": {
                            "left": 10,
                            "top": 20,
                            "right": 32,
                            "bottom": 10
                        },
                        "alignment": "centerLeft",
                        "height": 150,
                        "width": 300
                    }
                },
                {
                    "name": "Image",
                    "type": "network",
                    "width": 50,
                    "height": 50,
                    "src": "http://pic41.nipic.com/20140508/18609517_112216473140_2.jpg",
                    "fit": "fill"
                }
            ]
        }
    }
}
''';


class JsonUtils{

  static Widget judgeWidgetType(Map<String,dynamic> rootMap){
    Widget result;
    switch(rootMap["name"]){
      case "Container":
        result = parseContainer(rootMap);
        break;
      case "Text":
        result = parseText(rootMap);
        break;
      case 'Column':
        result = parseColumn(rootMap);
        break;
      case 'Expanded':
        result = parseExpanded(rootMap);
        break;
      case 'Image':
        result = parseImage(rootMap);
        break;
    }
    return result;
  }

  static Image parseImage(Map<String,dynamic> map){
    Image result;
    switch(map['type']){
      case 'network':
        result = Image.network(
            map['src'] ?? '',
            height: map['height'] * 1.0,
            width: map['width'] * 1.0,
            fit: parseBoxFit(map['fit']),
        );
        break;
    }
    return result;
  }

  static BoxFit parseBoxFit(String fit){
    BoxFit result;
    switch(fit){
      case 'cover':
        result = BoxFit.cover;
        break;
      case 'contain':
        result = BoxFit.contain;
        break;
      case 'fill':
        result = BoxFit.fill;
        break;
      case 'fitHeight':
        result = BoxFit.fitHeight;
        break;
      case 'fitWidth':
        result = BoxFit.fitWidth;
        break;
      case 'none':
        result = BoxFit.none;
        break;
      case 'scaleDown':
        result = BoxFit.scaleDown;
        break;
      default:result = BoxFit.none;
    }
    return result ;
  }

  static parseExpanded(Map<String,dynamic> map){
    return Expanded(
      flex: int.parse(map['flex'] ?? '1'),
      child: judgeWidgetType(map['child'] ?? '') ?? SizedBox(),
    );
  }

  static Text parseText(Map<String,dynamic> map){
    return Text(
      map['text'] ?? '',
      style: parseTextStyle(map['textStyle']),
    );
  }

  static parseTextStyle(Map<String,dynamic> map){
    return TextStyle(
        color: Color(int.parse(map['color'])),
        fontSize: map['fontSize'] ,
    );
  }

  static Container parseContainer(Map<String,dynamic> map){
    return Container(
      margin: parseEdgeInsetsGeometry(map['margin']) ?? EdgeInsets.all(0),
      padding: parseEdgeInsetsGeometry(map['padding']) ?? EdgeInsets.all(0),
      height: map['height'] * 1.0,
      width: map['width'] * 1.0,
      decoration: parseDecoration((map['decoration'])),
      child: (map == null || map['child'] == null ?? true) ? Container() : judgeWidgetType(map['child']),
    );
  }

  static Column parseColumn(Map<String,dynamic> map){
    return new Column(
      mainAxisAlignment: parseMainAxisAlignment(map['mainAxisAlignment']),
      crossAxisAlignment: parseCrossAxisAlignment(map['crossAxisAlignment']),
      children: (map['children'] as List).map((m){
        return judgeWidgetType(m);
      }).toList(),
    );
  }

  static CrossAxisAlignment parseCrossAxisAlignment(String crossAxisAlignment){
    CrossAxisAlignment result = CrossAxisAlignment.center;
    switch(crossAxisAlignment){
      case 'start':
        result = CrossAxisAlignment.start;
        break;
      case 'end':
        result = CrossAxisAlignment.end;
        break;
      case 'center':
        result = CrossAxisAlignment.center;
        break;
      case 'stretch':
        result = CrossAxisAlignment.stretch;
        break;
      case 'baseline':
        result = CrossAxisAlignment.baseline;
        break;
    }
    return result;
  }

  static MainAxisAlignment parseMainAxisAlignment(String mainAxisAlignment){
    MainAxisAlignment result = MainAxisAlignment.center;
    switch(mainAxisAlignment){
      case 'start':
        result = MainAxisAlignment.start;
          break;
      case 'end':
        result = MainAxisAlignment.end;
          break;
      case 'center':
        result = MainAxisAlignment.center;
          break;
      case 'spaceBetween':
        result = MainAxisAlignment.spaceBetween;
          break;
      case 'spaceAround':
        result = MainAxisAlignment.spaceAround;
          break;
      case 'spaceEvenly':
        result = MainAxisAlignment.spaceEvenly;
          break;
    }
    return result;
  }

  static EdgeInsetsGeometry parseEdgeInsetsGeometry(Map<String,dynamic> map){
    return EdgeInsets.only(
        left: (map['left'] ?? 0) * 1.0,
        top : (map['top'] ?? 0) * 1.0,
        right: (map['right'] ?? 0) * 1.0,
        bottom: (map['bottom'] ?? 0) * 1.0,
    );
  }

  static AlignmentGeometry parseAlignmentGeometry(String align){
    AlignmentGeometry a;
    if((align.isEmpty)){
      return Alignment.center;
    }
    switch(align){
      case 'centerLeft':
        a = Alignment.centerLeft;
        break;
      case 'center':
        a = Alignment.center;
        break;
      case 'topCenter':
        a = Alignment.topCenter;
        break;
      case 'centerRight':
        a = Alignment.centerRight;
        break;
      case 'bottomCenter':
        a = Alignment.bottomCenter;
        break;
      case 'topCenter':
        a = Alignment.topCenter;
        break;
      case 'topRight':
        a = Alignment.topRight;
        break;
      case 'bottomRight':
        a = Alignment.bottomRight;
        break;
      case 'bottomLeft':
        a = Alignment.bottomLeft;
        break;
      default:a = Alignment.center;
    }
    return a;
  }

  static Decoration parseDecoration(Map<String,dynamic> map){
    if(map == null || map['name'] == null ?? true)return null;
    Decoration result;
    switch(map['name']){
      case 'BoxDecoration':
        result = BoxDecoration(
            color: Color(int.parse(map['color'])),
            borderRadius: BorderRadius.circular(map['borderRadius'])
        );
        break;
    }
    return result;
  }
}





class JsonWidget extends StatelessWidget{



  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: JsonUtils.judgeWidgetType(json.decode(jsonStr)),
    );
  }

}










